package com.itboye.jinpuyiyuan.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.LaborDetailAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.InHosBean;
import com.itboye.jinpuyiyuan.bean.LaborInfoBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.MyListView;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class HospitalDetailActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;


    private LinearLayout inhosLl,//住院
            laborLl, //化验
            mediLl; //处方

    private TextView labor_time;
    private TextView labor_name,labor_sex,labor_age,labor_yltype;
    private MyListView labor_checks;

    private BranchAddressPresenter presenter;
    private String uid;
    private String report_type,report_id;

    private LaborInfoBean bean;
    private List<LaborInfoBean.Check.Detail> list;
    private LaborDetailAdapter adapter;
    private RelativeLayout titleRl;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_hospitaldetail);
        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("化验记录详情");

        list=new ArrayList<>();
        report_type=getIntent().getStringExtra("report_type");
        report_id=getIntent().getStringExtra("report_id");
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);

        mediLl.setVisibility(View.GONE);
        inhosLl.setVisibility(View.GONE);
        laborLl.setVisibility(View.VISIBLE);

        titleRl.setFocusable(true);
        titleRl.setFocusableInTouchMode(true);
        titleRl.requestFocus();

        adapter=new LaborDetailAdapter(this,list);
        labor_checks.setAdapter(adapter);
        showProgressDialog("数据加载中...",false);
        presenter.getLaborInfo(uid,report_type,report_id);



    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
        }
    }

    @Override
    public void update(Observable o, Object arg) {

        ResultEntity entity = handlerError(arg);

        if (entity!=null){
            try{ ;
                closeProgressDialog();

            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_laboratoryDetail_success){ //化验记录详情
                bean= (LaborInfoBean) entity.getData();
                initView(bean);
            }else  if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_laboratoryDetail_fail){
                MAlert.alert(entity.getMsg());

            }
        }
    }

    private void initView(LaborInfoBean bean) {
        labor_time.setText(bean.getReport_info().getReport_time());
        labor_name.setText(bean.getReport_info().getPatient_name());
        if ("0".equals(bean.getReport_info().getPatient_sex().trim())){ //女
            labor_sex.setText("女");
        }else if ("1".equals(bean.getReport_info().getPatient_sex().trim())){ //男
            labor_sex.setText("男");
        }
        labor_age.setText(bean.getReport_info().getPatient_age());
        labor_yltype.setText(bean.getReport_info().getMedical_insurannce_type());
        if (list.size()>0){
            list.clear();
        }
        list.addAll( bean.getCheck_list().get(0).getDetail());
        adapter.addData(list);
    }
}
